package com.sky.service.impl;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.mapper.ClassifyMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.Result;
import com.sky.service.ClassifyService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ClassifyServicelmpl implements ClassifyService {

    @Autowired
    private ClassifyMapper classifyMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 分类分页查询
     *
     * @return
     */
    public Long CountCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        Long count = (long) classifyMapper.countCategory(categoryPageQueryDTO);
        return count;
    }

    public List SearchCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        List<Category> List = new ArrayList<>();
        int Start = (categoryPageQueryDTO.getPage() - 1) * categoryPageQueryDTO.getPageSize();
        List = classifyMapper.GetCategotyList(categoryPageQueryDTO, Start);
        return List;
    }


    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    public int shanchu(Integer id) {
        List<Dish> dishes = dishMapper.getTypeDish(id);
        List<Setmeal> setmealList = setmealMapper.setSearch1(id);
        if (dishes.size() > 0) {
            return 0;//有彩品
        }
        if (setmealList.size() > 0) {
            return 2;//有套餐
        }
        classifyMapper.shanchu(id);
        return 1;//无菜品
    }


    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    public void xiugai(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        classifyMapper.xiugai(category);
    }


    /**
     * 启用禁用
     *
     * @param status
     * @return
     */
    public void qi(short status, Long id) {
        Category category = new Category();
        category.setStatus((int) status);
        category.setId(id);
        classifyMapper.qi(category);
    }


    /**
     * 新增
     *
     * @param categoryDTO
     * @return
     */
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(1);
        classifyMapper.add(category);
    }

    public ArrayList<Category> typeSearch(Integer type) {
        ArrayList<Category> category = new ArrayList<>();
        category = classifyMapper.typeSearch(type);
        return category;
    }

    public List<Category> userTypeSearch(Integer type) {
        ArrayList<Category> category = new ArrayList<>();
        category = classifyMapper.userTypeSearch(type);
        return category;
    }
}
